/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author user
 */
public class Connect {

    String url;
    Connection connect;

    public Connect() throws ClassNotFoundException, SQLException {
        url = "com.mysql.jdbc.Driver";
        Class.forName(url);
        String str = "jdbc:mysql://localhost:3306/phantrang";
        connect = DriverManager.getConnection(str, "root", "root");

    }

    public List<User> getAll(int pageNumber) throws SQLException {
        int offset = (pageNumber - 1) * 5;

       List<User> result = new ArrayList<User>();

        Statement stm = connect.createStatement();
        String sql = "select * from users limit 5 offset " + offset;
        ResultSet rs = stm.executeQuery(sql);
        while (rs.next()) {
            int id = rs.getInt(1);
            String name = rs.getString(2);
            String pass = rs.getString(3);

            User user = new User(id, name, pass);
            result.add(user);
        }


        return result;
    }

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Connect c = new Connect();
        List<User> users =  c.getAll(2);
        for (User user : users) {
            System.out.println(user.getId());
        }
    }
}
